package com.ktjiaoyu.crm.service.impl;

import com.ktjiaoyu.crm.entity.CustLost;
import com.ktjiaoyu.crm.repository.LostRepository;
import com.ktjiaoyu.crm.service.ILostService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
@Service(value = "lostService")
public class LostServiceImpl implements ILostService{

    @Resource
    private LostRepository lostRepository;
    @Override
    public Page<CustLost> findAllLost(String custName, Pageable pageable) {
        Specification<CustLost> specification = new Specification<CustLost>() {

            @Override
            public Predicate toPredicate(Root<CustLost> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(custName != null && !custName.equals("")){
                    predicates.add(criteriaBuilder.like(root.get("custName"),"%"+custName+"%"));
                }

                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return lostRepository.findAll(specification,pageable);
    }

}
